package com.linxianqin.aop.aspect;


import lombok.extern.slf4j.Slf4j;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.*;
import org.springframework.stereotype.Component;
/**
 * 功能描述：
 *
 * @author lxq
 * @version 1.00
 * @Date 2020/12/11
 */
@Component
@Slf4j
@Aspect
public class TestAopClassAspect {
    //该类下所有方法都会被切面
    @Pointcut("execution(public * com.linxianqin.aop.controller.TestAopClassController.*(..))")
    public void pointcut(){}

    @Before("pointcut()")
    public void doBefore(JoinPoint joinPoint)throws Throwable{
        log.info("doBefore");
    }

    //后置异常通知
    @AfterThrowing("pointcut()")
    public void throwss(JoinPoint jp){
        log.info("方法异常时执行.....");
    }

    @After("pointcut()")
    public void doAfter(JoinPoint joinPoint)throws Throwable{
        log.info("doAfter");
    }

    @Around("pointcut()")
    public Object around(ProceedingJoinPoint pjp) throws Throwable{
        log.info("around...........");
        Class<?> aclass = pjp.getTarget().getClass();//获取被织入增强处理的目标对象，在获取目标类的class
        String methodName = pjp.getSignature().getName();//获取目标方法的签名，在获取目标方法的名
        log.info("methodName:{}",methodName);
        try {
            Object o =  pjp.proceed();//方法执行，o返回值
            System.out.println("方法环绕proceed，结果是 :" + o);
            return o;
        } catch (Throwable e) {
            throw e;
        }
    }

    @AfterReturning(returning = "ret", pointcut = "pointcut()")
    public void doAfterReturning(Object ret) throws Throwable {
        // 处理完请求，返回内容
        log.info("方法的返回值 : {}" , ret);
    }

}
